package net.entelijan.cobean.examples.showcase.bind.text;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JOptionPane;

import net.entelijan.cobean.bind.impl.BoundPropBinding;
import net.entelijan.cobean.core.ICobean;
import net.entelijan.cobean.data.literature.ILitService;
import net.entelijan.cobean.data.literature.LitAuthor;
import net.entelijan.cobean.text.DefaultTextInitializer;
import net.entelijan.cobean.text.converter.DateTextConverter;
import net.entelijan.cobean.text.converter.LongTextConverter;
import net.entelijan.scf.IShowcase;

public class DoubleBindTextShowcase implements IShowcase<DoubleBindTextPanel> {

	private ILitService service = null;

	private LitAuthor model = new LitAuthor();

	public DoubleBindTextShowcase() {
		super();
	}

	@Override
	public void init(final DoubleBindTextPanel view) {

		model = service.loadAllAuthors().get(7);
		{
			DefaultTextInitializer<String> ini = new DefaultTextInitializer<String>();
			ini.setCoTextComponent(view.getNameUpTextField());
			ini.setCoFailureComponent(view.getNameFailureUpTextField());
			ini.setCoResetButton(view.getResetButton());
			ICobean<String> cobean = ini.initialize();

			BoundPropBinding binding = new BoundPropBinding();
			binding.setModelPath("name");
			binding.setCompProperty("value");
			model = binding.bind(model, cobean);

		}
		{
			DefaultTextInitializer<Date> ini = new DefaultTextInitializer<Date>();
			ini.setCoTextComponent(view.getBirthdayUpTextField());
			ini.setCoFailureComponent(view.getBirthdayFailureUpTextField());
			ini.setCoResetButton(view.getResetButton());
			ini.setConverter(new DateTextConverter("dd.MM.yyyy"));
			ICobean<Date> cobean = ini.initialize();

			BoundPropBinding binding = new BoundPropBinding();
			binding.setModelPath("birthday");
			binding.setCompProperty("value");
			model = binding.bind(model, cobean);
		}
		{
			DefaultTextInitializer<Long> ini = new DefaultTextInitializer<Long>();
			ini.setCoTextComponent(view.getIdUpTextField());
			ini.setCoFailureComponent(view.getIdFailureUpTextField());
			ini.setCoResetButton(view.getResetButton());
			ini.setConverter(new LongTextConverter(false));
			ICobean<Long> cobean = ini.initialize();

			BoundPropBinding binding = new BoundPropBinding();
			binding.setModelPath("id");
			binding.setCompProperty("value");
			model = binding.bind(model, cobean);
		}
		{
			DefaultTextInitializer<String> ini = new DefaultTextInitializer<String>();
			ini.setCoTextComponent(view.getNameDownTextField());
			ini.setCoFailureComponent(view.getNameFailureDownTextField());
			ini.setCoResetButton(view.getResetButton());
			ICobean<String> cobean = ini.initialize();

			BoundPropBinding binding = new BoundPropBinding();
			binding.setModelPath("name");
			binding.setCompProperty("value");
			model = binding.bind(model, cobean);

		}
		{
			DefaultTextInitializer<Date> ini = new DefaultTextInitializer<Date>();
			ini.setCoTextComponent(view.getBirthdayDownTextField());
			ini.setCoFailureComponent(view.getBirthdayFailureDownTextField());
			ini.setCoResetButton(view.getResetButton());
			ini.setConverter(new DateTextConverter("dd.MM.yyyy"));
			ICobean<Date> cobean = ini.initialize();

			BoundPropBinding binding = new BoundPropBinding();
			binding.setModelPath("birthday");
			binding.setCompProperty("value");
			model = binding.bind(model, cobean);
		}
		{
			DefaultTextInitializer<Long> ini = new DefaultTextInitializer<Long>();
			ini.setCoTextComponent(view.getIdDownTextField());
			ini.setCoFailureComponent(view.getIdFailureDownTextField());
			ini.setCoResetButton(view.getResetButton());
			ini.setConverter(new LongTextConverter(false));
			ICobean<Long> cobean = ini.initialize();

			BoundPropBinding binding = new BoundPropBinding();
			binding.setModelPath("id");
			binding.setCompProperty("value");
			model = binding.bind(model, cobean);
		}

		view.getShowObjectButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(view, model);
			}

		});

		view.getUppercaseButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				final String name = model.getName();
				if (name != null) {
					model.setName(name.toUpperCase());
				}
			}
		});

		view.getLowercaseButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				final String name = model.getName();
				if (name != null) {
					model.setName(name.toLowerCase());
				}
			}
		});

	}

	public ILitService getService() {
		return service;
	}

	public void setService(ILitService service) {
		this.service = service;
	}

	public LitAuthor getModel() {
		return model;
	}

	public void setModel(LitAuthor model) {
		this.model = model;
	}

}
